fix: replace HTTP status string parsing with structured HTTPError#83
Merged
Conversation
Introduce HTTPError(status, method, path, body) deriving from HAClientError. - _request() now raises HTTPError for all non-401 HTTP failures instead of encoding the status code into a generic HAClientError message string. - get_state() checks err.status == 404 instead of parsing 'HTTP 404' out of str(err), eliminating the fragile string-matching pattern. - AuthenticationError (401) is unchanged. - HTTPError is exported from the top-level haclient package. - Tests added: HTTPError attributes, get_state returns None only for real 404 responses, and get_state re-raises non-404 HTTP errors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #79
Validity Assessment
Valid bug.
get_state()detected missing entities by parsing"HTTP 404"out of an exception message string. This is fragile: a message format change or a coincidental match in a response body could silently alter behavior. Status codes are structured data and should be treated as such.Fix
New exception:
HTTPErrorAdded
HTTPError(status, method, path, body)toexceptions.py, derived fromHAClientError. Carries the HTTP status code, method, path, and body as typed attributes. Thestr()representation is backward-compatible ("HTTP 404 from GET /api/states/foo: ...").HTTPErroris exported from the top-levelhaclientpackage._request()—rest_aiohttp.pyRaises
HTTPErrorinstead of genericHAClientErrorfor all non-401 HTTP failures.get_state()—rest_aiohttp.pyBefore:
After:
Tests / Checks Run
test_request_server_error— updated to assert the raised exception isHTTPErrorwith correctstatus,method,pathattributes, and that it is still an instance ofHAClientError.test_http_error_attributes— new: verifies all structured attributes and the string representation.test_get_state_returns_none_only_for_404— new: verifiesget_state()returnsNoneonly for a real 404, not for other scenarios.test_get_state_reraises_non_404_http_error— new: verifiesget_state()re-raisesHTTPErrorwhen status is not 404 (e.g. 500).Suite results:
pytest: 307 passed, coverage 97.16% (threshold 95%)ruff check+ruff format --check: all checks passedmypy src: no issues found